﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace DemoGame1
{
    public class Setting : Menu
    {
        myGame game;
        int _menuItems;
        KeyboardState oldState;
        Color Normal = Color.Silver;
        Color Hilite = Color.Red;
        MyButton background;

        static int MaxVolumeMusicTheme = 10;
        static int MaxVolumeFX = 10;

        int volumeMusicTheme;

        public int VolumeMusicTheme
        {
            get { return volumeMusicTheme; }
            set
            {
                if (0 <= value && value <= MaxVolumeMusicTheme)
                    volumeMusicTheme = value;
            }
        }

        int volumeFX;

        public int VolumeFX
        {
            get { return volumeFX; }
            set
            {
                if (0 <= value && value <= MaxVolumeFX)
                    volumeFX = value;
            }
        }

        int difficult = 0;

        public int Difficult
        {
            get { return difficult; }
            set
            {
                if (0 <= value && value <= 2)
                    difficult = value;
            }
        }

        int theme = 0;

        public int Theme
        {
            get { return theme; }
            set
            {
                if (0 <= value && value <= 1)
                    theme = value;
            }
        }

        private int selectedIndex = 0;

        public Setting()
        {
        }

        public void LoadReSource(myGame _g, string strXML)
        {
            game = _g;
            myButtons = new List<MyButton>();
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlElement backGround = (XmlElement)doc.SelectSingleNode("//BackGround");
            //MyButton backGroundButton;
            if (backGround != null)
            {
                background = new MyButton(game.Content, backGround);
            }
            //add char
            XmlElement buttons = (XmlElement)doc.SelectSingleNode("//Buttons");
            foreach (XmlNode button in buttons.ChildNodes)
            {
                MyButton bt = new MyButton(game.Content, (XmlElement)button);
                myButtons.Add(bt);
            }
            _menuItems = 4;
            UpdateValue();
        }

        public void LoadSetting()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(XMLDataProvider.strData);
            XmlElement settingXML = (XmlElement)doc.SelectSingleNode("//Setting");

            foreach (XmlElement set in settingXML.ChildNodes)
            {
                if (set.Name == "Music")
                {
                    this.VolumeMusicTheme = Convert.ToInt16(set.InnerText.Trim());
                }

                if (set.Name == "SoundFX")
                {
                    this.VolumeFX = Convert.ToInt16(set.InnerText.Trim());
                }

                if (set.Name == "Difficult")
                {
                    this.Difficult = Convert.ToInt16(set.InnerText.Trim());
                }
                if (set.Name == "Theme")
                {
                    if (Convert.ToInt16(set.InnerText.Trim()) == 0)
                    {
                        myGame.theme = Global.THEMES.RYU_THEME;
                    }
                    if (Convert.ToInt16(set.InnerText.Trim()) == 1)
                    {
                        myGame.theme = Global.THEMES.KEN_THEME;
                    }
                }
            }
            UpdateValue();
        }

        public void SaveSetting()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(XMLDataProvider.strData);
            XmlElement settingXML = (XmlElement)doc.SelectSingleNode("//Setting");

            foreach (XmlElement set in settingXML.ChildNodes)
            {
                if (set.Name == "Music")
                {
                    set.InnerText = this.volumeMusicTheme.ToString();
                }

                if (set.Name == "SoundFX")
                {
                    set.InnerText = this.volumeFX.ToString();
                }

                if (set.Name == "Difficult")
                {
                    if (Global.Difficult == Global.DifficultLevel.NORMAL)
                    {
                        set.InnerText = 0.ToString();
                    }
                    if (Global.Difficult == Global.DifficultLevel.HARD)
                    {
                        set.InnerText = 1.ToString();
                    }
                }
                if (set.Name == "Theme")
                {
                    if (myGame.theme == Global.THEMES.RYU_THEME)
                    {
                        set.InnerText = 0.ToString();
                    }
                    if (myGame.theme == Global.THEMES.KEN_THEME)
                    {
                        set.InnerText = 1.ToString();
                    }
                }
            }
            try
            {
                doc.Save(XMLDataProvider.strData);
            }
            catch (XmlException xe)
            {
            }
        }

        public override void HandleKeyInput()
        {
            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    selectedIndex = (selectedIndex + 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Up))
            {
                if (!oldState.IsKeyDown(Keys.Up))
                {
                    selectedIndex = (selectedIndex + _menuItems - 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Left))
            {
                if (!oldState.IsKeyDown(Keys.Left))
                {
                    if (selectedIndex == 0)
                    {
                        myButtons[5].SetNextFrame(-1);
                        volumeMusicTheme = myButtons[5].Sprites[0].iTextures;
                        myGame.mediaControler.Volume = (float)volumeMusicTheme / 10;
                    }
                    if (selectedIndex == 1)
                    {
                        myButtons[6].SetNextFrame(-1);
                        volumeFX = myButtons[6].Sprites[0].iTextures;
                        myGame.mediaControler.VolumeFX = (float)volumeFX / 10;
                    }
                    if (selectedIndex == 2)
                    {
                        Difficult = difficult - 1;
                        UpdateValue();
                    }
                    if (selectedIndex == 3)
                    {
                        myButtons[8].SetNextFrame(-1);
                        ChangeTheme();
                    }
                }
            }

            if (newState.IsKeyDown(Keys.Right))
            {
                if (!oldState.IsKeyDown(Keys.Right))
                {
                    if (selectedIndex == 0)
                    {
                        myButtons[5].SetNextFrame(1);
                        volumeMusicTheme = myButtons[5].Sprites[0].iTextures;
                        myGame.mediaControler.Volume = (float)volumeMusicTheme / 10;
                    }
                    if (selectedIndex == 1)
                    {
                        myButtons[6].SetNextFrame(1);
                        volumeFX = myButtons[6].Sprites[0].iTextures;
                        myGame.mediaControler.VolumeFX = (float)volumeFX / 10;
                    }
                    if (selectedIndex == 2)
                    {
                        Difficult = difficult + 1;
                        UpdateValue();
                    }
                    if (selectedIndex == 3)
                    {
                        myButtons[8].SetNextFrame(1);
                        ChangeTheme();
                    }
                }
            }

            if (newState.IsKeyDown(Keys.Back))
            {
                if (!oldState.IsKeyDown(Keys.Back))
                {
                    myGame.currentScreen = CurrentScreen.StartMenu;
                    SaveSetting();
                }

            }
            oldState = newState;
        }

        protected void ChangeTheme()
        {
            if (myButtons[8].Sprites[0].iTextures == 0)
            {
                myGame.theme = Global.THEMES.RYU_THEME;
            }
            if (myButtons[8].Sprites[0].iTextures == 1)
            {
                myGame.theme = Global.THEMES.KEN_THEME;
            }
            game.InitMenu();
        }

        public override void Update(GameTime gameTime)
        {
            HandleKeyInput();
            //update pointer
            myButtons[3].Update(gameTime);
        }
        public void UpdateValue()
        {
            myButtons[5].Sprites[0].iTextures = volumeMusicTheme;
            myGame.mediaControler.Volume = (float)volumeMusicTheme / 10;

            myButtons[6].Sprites[0].iTextures = volumeFX;
            myGame.mediaControler.VolumeFX = (float)volumeFX / 10;

            myButtons[7].Sprites[0].iTextures = difficult;

            switch (difficult)
            {
                case 0:
                    Global.Difficult = Global.DifficultLevel.NORMAL;
                    break;
                case 1:
                    Global.Difficult = Global.DifficultLevel.HARD;
                    break;
            }


            if (myGame.theme == Global.THEMES.RYU_THEME)
            {
                myButtons[8].Sprites[0].iTextures = 0;
            }
            if (myGame.theme == Global.THEMES.KEN_THEME)
            {
                myButtons[8].Sprites[0].iTextures = 1;
            }
        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (background != null)
                background.Draw(sb, gameTime);

            Vector2 indent = new Vector2(0, 80);
            for (int i = 0; i < myButtons.Count; i++)
            {
                switch (i)
                {
                    case 4:
                        myButtons[i].Draw(sb, gameTime, myButtons[i].TopLeft + selectedIndex * indent, myButtons[i].Size);
                        break;
                    default:
                        myButtons[i].Draw(sb, gameTime);
                        break;
                }

            }
        }
    }
}
